package com.buddy.sds.auth.oauth2.config;

import com.buddy.sds.auth.oauth2.token.TokenManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@Slf4j
public class OAuth2Config {



    @Autowired
    private AutowireCapableBeanFactory capableBeanFactory;

    @Autowired
    private OAuth2ConfigProperties oAuth2ConfigProperties;

    @Bean
    public TokenManager tokenManager(){

        Class<TokenManager> tokenManagerClass = oAuth2ConfigProperties.getTokenManager();
        TokenManager tokenManager = null;
        try {

            tokenManager = tokenManagerClass.newInstance();

            capableBeanFactory.autowireBean(tokenManager);

            return tokenManager;

        } catch (Exception e) {

            log.error(e.getLocalizedMessage());
            log.error("tokenManager实例化失败");

            throw new RuntimeException(e);
        }
    }
}
